{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>candle_begin_time</th>\n",
       "      <th>symbol</th>\n",
       "      <th>open</th>\n",
       "      <th>high</th>\n",
       "      <th>low</th>\n",
       "      <th>close</th>\n",
       "      <th>volume</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2018-01-24 16:00:00</td>\n",
       "      <td>AIDBTC</td>\n",
       "      <td>1.97e-04</td>\n",
       "      <td>1.97e-04</td>\n",
       "      <td>3.01e-05</td>\n",
       "      <td>1.35e-04</td>\n",
       "      <td>27949.99</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2018-01-24 17:00:00</td>\n",
       "      <td>AIDBTC</td>\n",
       "      <td>7.90e-05</td>\n",
       "      <td>9.99e-05</td>\n",
       "      <td>4.70e-05</td>\n",
       "      <td>5.60e-05</td>\n",
       "      <td>51104.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2018-01-24 18:00:00</td>\n",
       "      <td>AIDBTC</td>\n",
       "      <td>6.30e-05</td>\n",
       "      <td>7.47e-05</td>\n",
       "      <td>5.01e-05</td>\n",
       "      <td>7.18e-05</td>\n",
       "      <td>106084.15</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2018-01-24 19:00:00</td>\n",
       "      <td>AIDBTC</td>\n",
       "      <td>7.17e-05</td>\n",
       "      <td>7.33e-05</td>\n",
       "      <td>6.11e-05</td>\n",
       "      <td>7.04e-05</td>\n",
       "      <td>60643.77</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2018-01-24 20:00:00</td>\n",
       "      <td>AIDBTC</td>\n",
       "      <td>7.00e-05</td>\n",
       "      <td>7.70e-05</td>\n",
       "      <td>6.10e-05</td>\n",
       "      <td>6.10e-05</td>\n",
       "      <td>26093.50</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2145</th>\n",
       "      <td>2018-01-24 19:00:00</td>\n",
       "      <td>ZRXUSD</td>\n",
       "      <td>1.60e+00</td>\n",
       "      <td>1.62e+00</td>\n",
       "      <td>1.58e+00</td>\n",
       "      <td>1.62e+00</td>\n",
       "      <td>8728.98</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2146</th>\n",
       "      <td>2018-01-24 20:00:00</td>\n",
       "      <td>ZRXUSD</td>\n",
       "      <td>1.61e+00</td>\n",
       "      <td>1.66e+00</td>\n",
       "      <td>1.61e+00</td>\n",
       "      <td>1.66e+00</td>\n",
       "      <td>45549.64</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2147</th>\n",
       "      <td>2018-01-24 21:00:00</td>\n",
       "      <td>ZRXUSD</td>\n",
       "      <td>1.65e+00</td>\n",
       "      <td>1.70e+00</td>\n",
       "      <td>1.65e+00</td>\n",
       "      <td>1.67e+00</td>\n",
       "      <td>95792.54</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2148</th>\n",
       "      <td>2018-01-24 22:00:00</td>\n",
       "      <td>ZRXUSD</td>\n",
       "      <td>1.66e+00</td>\n",
       "      <td>1.68e+00</td>\n",
       "      <td>1.64e+00</td>\n",
       "      <td>1.65e+00</td>\n",
       "      <td>21623.11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2149</th>\n",
       "      <td>2018-01-24 23:00:00</td>\n",
       "      <td>ZRXUSD</td>\n",
       "      <td>1.65e+00</td>\n",
       "      <td>1.68e+00</td>\n",
       "      <td>1.65e+00</td>\n",
       "      <td>1.68e+00</td>\n",
       "      <td>1202.25</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>2150 rows × 7 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "        candle_begin_time  symbol      open      high       low     close     volume\n",
       "0     2018-01-24 16:00:00  AIDBTC  1.97e-04  1.97e-04  3.01e-05  1.35e-04   27949.99\n",
       "1     2018-01-24 17:00:00  AIDBTC  7.90e-05  9.99e-05  4.70e-05  5.60e-05   51104.00\n",
       "2     2018-01-24 18:00:00  AIDBTC  6.30e-05  7.47e-05  5.01e-05  7.18e-05  106084.15\n",
       "3     2018-01-24 19:00:00  AIDBTC  7.17e-05  7.33e-05  6.11e-05  7.04e-05   60643.77\n",
       "4     2018-01-24 20:00:00  AIDBTC  7.00e-05  7.70e-05  6.10e-05  6.10e-05   26093.50\n",
       "...                   ...     ...       ...       ...       ...       ...        ...\n",
       "2145  2018-01-24 19:00:00  ZRXUSD  1.60e+00  1.62e+00  1.58e+00  1.62e+00    8728.98\n",
       "2146  2018-01-24 20:00:00  ZRXUSD  1.61e+00  1.66e+00  1.61e+00  1.66e+00   45549.64\n",
       "2147  2018-01-24 21:00:00  ZRXUSD  1.65e+00  1.70e+00  1.65e+00  1.67e+00   95792.54\n",
       "2148  2018-01-24 22:00:00  ZRXUSD  1.66e+00  1.68e+00  1.64e+00  1.65e+00   21623.11\n",
       "2149  2018-01-24 23:00:00  ZRXUSD  1.65e+00  1.68e+00  1.65e+00  1.68e+00    1202.25\n",
       "\n",
       "[2150 rows x 7 columns]"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "\n",
    "# 对print出的数据格式进行修正\n",
    "pd.set_option('expand_frame_repr', False)  # 当列太多时不换行\n",
    "pd.set_option('precision', 2)  # 浮点数的精度\n",
    "pd.set_option('display.max_rows', 10) # 显示的最大行数\n",
    "\n",
    "# =====导入数据\n",
    "df = pd.read_csv(\n",
    "    r'C:\\notebooks\\quantitative_trading_notes\\data\\BITFINEX-1H-data-20180124.csv',\n",
    "    skiprows=1,\n",
    "    # nrows=15,\n",
    ")\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2018-01-24 16:00:00\n",
      "<class 'str'>\n",
      "2018-01-24 16:00:00\n",
      "<class 'pandas._libs.tslibs.timestamps.Timestamp'>\n"
     ]
    }
   ],
   "source": [
    "print(df.at[0, 'candle_begin_time'])\n",
    "print(type(df.at[0, 'candle_begin_time']))\n",
    "\n",
    "# to_datetime很智能 能识别不同的格式\n",
    "df['candle_begin_time'] = pd.to_datetime(df['candle_begin_time'])  # 将交易日期由字符串改为时间变量\n",
    "print(df.at[0, 'candle_begin_time'])\n",
    "print(type(df.at[0, 'candle_begin_time']))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0       2018\n",
      "1       2018\n",
      "2       2018\n",
      "3       2018\n",
      "4       2018\n",
      "        ... \n",
      "2145    2018\n",
      "2146    2018\n",
      "2147    2018\n",
      "2148    2018\n",
      "2149    2018\n",
      "Name: candle_begin_time, Length: 2150, dtype: int64\n",
      "0       4\n",
      "1       4\n",
      "2       4\n",
      "3       4\n",
      "4       4\n",
      "       ..\n",
      "2145    4\n",
      "2146    4\n",
      "2147    4\n",
      "2148    4\n",
      "2149    4\n",
      "Name: candle_begin_time, Length: 2150, dtype: int64\n",
      "0       24\n",
      "1       24\n",
      "2       24\n",
      "3       24\n",
      "4       24\n",
      "        ..\n",
      "2145    24\n",
      "2146    24\n",
      "2147    24\n",
      "2148    24\n",
      "2149    24\n",
      "Name: candle_begin_time, Length: 2150, dtype: int64\n",
      "0       2\n",
      "1       2\n",
      "2       2\n",
      "3       2\n",
      "4       2\n",
      "       ..\n",
      "2145    2\n",
      "2146    2\n",
      "2147    2\n",
      "2148    2\n",
      "2149    2\n",
      "Name: candle_begin_time, Length: 2150, dtype: int64\n",
      "********************\n",
      "0       2\n",
      "1       2\n",
      "2       2\n",
      "3       2\n",
      "4       2\n",
      "       ..\n",
      "2145    2\n",
      "2146    2\n",
      "2147    2\n",
      "2148    2\n",
      "2149    2\n",
      "Name: candle_begin_time, Length: 2150, dtype: int64\n",
      "0       Wednesday\n",
      "1       Wednesday\n",
      "2       Wednesday\n",
      "3       Wednesday\n",
      "4       Wednesday\n",
      "          ...    \n",
      "2145    Wednesday\n",
      "2146    Wednesday\n",
      "2147    Wednesday\n",
      "2148    Wednesday\n",
      "2149    Wednesday\n",
      "Name: candle_begin_time, Length: 2150, dtype: object\n",
      "0       31\n",
      "1       31\n",
      "2       31\n",
      "3       31\n",
      "4       31\n",
      "        ..\n",
      "2145    31\n",
      "2146    31\n",
      "2147    31\n",
      "2148    31\n",
      "2149    31\n",
      "Name: candle_begin_time, Length: 2150, dtype: int64\n",
      "0       False\n",
      "1       False\n",
      "2       False\n",
      "3       False\n",
      "4       False\n",
      "        ...  \n",
      "2145    False\n",
      "2146    False\n",
      "2147    False\n",
      "2148    False\n",
      "2149    False\n",
      "Name: candle_begin_time, Length: 2150, dtype: bool\n"
     ]
    }
   ],
   "source": [
    "print(df['candle_begin_time'].dt.year)  # 输出这个日期的年份。相应的month是月份，day是天数，还有hour, minute, second\n",
    "print(df['candle_begin_time'].dt.week)  # 这一天是一年当中的第几周\n",
    "print(df['candle_begin_time'].dt.dayofyear)  # 这一天是一年当中的第几天\n",
    "print(df['candle_begin_time'].dt.dayofweek)  # 这一天是这一周当中的第几天，0代表星期一\n",
    "print('*' * 20)\n",
    "print(df['candle_begin_time'].dt.weekday)  # 和上面函数相同，更加常用\n",
    "print(df['candle_begin_time'].dt.weekday_name)  # 和上面函数相同，返回的是星期几的英文，用于报表的制作。\n",
    "print(df['candle_begin_time'].dt.days_in_month)  # 这一天所在月份有多少天\n",
    "print(df['candle_begin_time'].dt.is_month_start)  # 这一天是否是该月的开头，是否存在is_month_end？"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0      2018-01-24 17:00:00\n",
      "1      2018-01-24 18:00:00\n",
      "2      2018-01-24 19:00:00\n",
      "3      2018-01-24 20:00:00\n",
      "4      2018-01-24 21:00:00\n",
      "               ...        \n",
      "2145   2018-01-24 20:00:00\n",
      "2146   2018-01-24 21:00:00\n",
      "2147   2018-01-24 22:00:00\n",
      "2148   2018-01-24 23:00:00\n",
      "2149   2018-01-25 00:00:00\n",
      "Name: candle_begin_time, Length: 2150, dtype: datetime64[ns]\n",
      "0      1 days\n",
      "1      1 days\n",
      "2      1 days\n",
      "3      1 days\n",
      "4      1 days\n",
      "        ...  \n",
      "2145   1 days\n",
      "2146   1 days\n",
      "2147   1 days\n",
      "2148   1 days\n",
      "2149   1 days\n",
      "Name: candle_begin_time, Length: 2150, dtype: timedelta64[ns]\n"
     ]
    }
   ],
   "source": [
    "# 增加一天\n",
    "print(df['candle_begin_time'] + pd.Timedelta(hours=1))  # 增加一天，Timedelta用于表示时间差数据，[weeks, days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds]\n",
    "print((df['candle_begin_time'] + pd.Timedelta(days=1)) - df['candle_begin_time'])  # 增加一天然后再减去今天的日期"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
